package com.qf.smsplatfrom.config;

import com.qf.smsplatfrom.cache.MenuCaChe;
import com.qf.smsplatfrom.mapper.MenuMapper;
import com.qf.smsplatfrom.pojo.TMenu;
import com.qf.smsplatfrom.sucurity.MyFailHandler;
import com.qf.smsplatfrom.sucurity.MySuccessHandler;
import com.qf.smsplatfrom.sucurity.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Configuration//开启 springsecurituy(springsecurity 的配置类,是一个适配器模式,主要做转换用的)
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    private MyUserDetailsService myUserDetailsService;

    @Autowired
    public void setMyUserDetailsService(MyUserDetailsService myUserDetailsService) {
        this.myUserDetailsService = myUserDetailsService;
    }

    @Autowired
    public void setMySuccessHandler(MySuccessHandler mySuccessHandler) {
        this.mySuccessHandler = mySuccessHandler;
    }

    private MySuccessHandler mySuccessHandler;
    private MenuCaChe menuCaChe;
    private MyFailHandler myFailHandler;
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public void setbCryptPasswordEncoder(BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Autowired
    public void setMyFailHandler(MyFailHandler myFailHandler) {
        this.myFailHandler = myFailHandler;
    }

    @Autowired
    public void setMenuCaChe(MenuCaChe menuCaChe) {
        this.menuCaChe = menuCaChe;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
        //配置了用户的查询方式以及密码的比较方式
        auth.userDetailsService(myUserDetailsService).passwordEncoder(bCryptPasswordEncoder);//设置用户该如何查询
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();//允许跨域请求，特别是移动端的
        //配置请求地址与与标识之间的运算关系
        List<TMenu> menuList = menuCaChe.getAllData();//拿到所有的menu对象

        menuList.forEach(tMenu -> {
            //设置每个地址需要的权限
            if (!ObjectUtils.isEmpty(tMenu.getUrl())) {
                try {
                    //antMatchers匹配地址里边哟两个权限，第一个是匹配地址
                    //hasAnyAuthority别名
                    http.authorizeRequests().antMatchers(tMenu.getUrl()).hasAnyAuthority(tMenu.getPerms());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //我们上面设置每个地址具体的配置,有可能有些地址还没有添加到 menu表中,或者忘记了,那么我们默认这些地址必须登录才可以
        http.authorizeRequests().anyRequest().authenticated()
                .and().formLogin().loginProcessingUrl("/login")
                .successHandler(mySuccessHandler)
                .failureHandler(myFailHandler)
                .permitAll()//这是相当于设置我们的接口地址为login,springsecority会自动生成这个接口，permitAll为为该接口进行放行
                //默认名字为username,password但是我们可以自己设定 .usernameParameter("aaaaa").passwordParameter()
                .and().logout().logoutUrl("logout");
        //logoutSuccessUrl("/")相当与推出后去那个地址


    }
}
